import useRouter from 'koa-router';
import { sign, verify } from '../utils/jwt.js'
import { getDetailData, getTeacherData, saveTeacherData } from '../controllers/DataService.js'

const router = useRouter()

// page页详情页数据请求接口
router.get('/page/detail', async (ctx) => {
    const res = await getDetailData() // 查询结果为数组
    ctx.body = {
        code: '8000',
        data: res,
        msg: 'success'
    }
})

// 我的页面接口
router.get('/page/myInfo', async (ctx) => {
    const id = ctx.query.id
    // console.log(id);
    const res = await getTeacherData(id)
    // console.log(res); // 查询结果为数组
    if (res.length > 0) {
        ctx.body = {
            code: '8000',
            data: res,
            msg: '数据查询成功'
        }
    } else {
        ctx.body = {
            code: '8006',
            msg: '教员信息查询失败'
        }
    }

})

// 我的页面用户保存数据接口
router.post('/page/saveInfo', async (ctx) => {
    const id = ctx.request.body.id
    // console.log(id);
    const res = await saveTeacherData(id, ctx.request.body.username, ctx.request.body.sex, ctx.request.body.colleage, ctx.request.body.specialized, ctx.request.body.address, ctx.request.body.subject, ctx.request.body.freeTime, ctx.request.body.expectedWages, ctx.request.body.certificate)
    if (res) {
        ctx.body = {
            code: '8000',
            msg: '数据保存成功'
        }
    } else {
        ctx.body = {
            code: '8007',
            msg: '教员信息保存失败'
        }
    }


})

export default router